/*
 *  Map.cpp
 *  SquirrelAdven
 *
 *  Created by Wen Xu on 3/14/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "Map.h"
#include "OBJtoTriMesh.h"

Map::Map(std::string filename, std::string wmtfFilename) {
	objFilename = filename;
	OBJtoTriMesh objToTriMesh;
	mapMesh = objToTriMesh.CreateMesh("./", objFilename, wmtfFilename);
	computeSize();
}

Map::~Map() {
	
}

TriMesh * Map::getMapMesh() {
	return mapMesh;
}

void Map::computeSize() {
	float min_x, max_x, min_y, max_y, min_z, max_z;
	int i;
	int numVertices = mapMesh->GetNumVertices();
	
	// get the first vertice
	min_x = max_x = mapMesh->GetPosition(0)[0];
	min_y = max_y = mapMesh->GetPosition(0)[1];
	min_z = max_z = mapMesh->GetPosition(0)[2];
	
	for (i = 1; i < numVertices; i++) {
		Float3 v = mapMesh->GetPosition(i);
		
		// min x
		if (v[0] < min_x) {
			min_x = v[0];
		}
		
		// max x
		if (v[0] > max_x) {
			max_x = v[0];
		}
		
		// min y
		if (v[1] < min_y) {
			min_y = v[1];
		}
		
		// max y
		if (v[1] > max_y) {
			max_y = v[1];
		}
		
		// min z
		if (v[2] < min_z) {
			min_z = v[2];
		}
		
		// max z
		if (v[2] > max_z) {
			max_z = v[2];
		}
	}
	
	// compute length
	len_x = max_x - min_x;
	len_y = max_y - min_y;
	len_z = max_z - min_z;
}

float Map::getOriginalLengthX() {
	return len_x;
}

float Map::getOriginalLengthY() {
	return len_y;
}

float Map::getOriginalLengthZ() {
	return len_z;
}